#ifndef _SRC_NET_TIMER_H_
#define _SRC_NET_TIMER_H_

#include <atomic>

#include "src/base/time_stamp.h"
#include "src/base/utils.h"
#include "src/net/callback.h"

namespace fio66::net {
///
/// Internal class for timer event.
///
class Timer {
  DISALLOW_COPY_AND_ASSIGN(Timer)
 public:
  Timer(TimerCallback cb, Timestamp when, double interval)
      : callback_(std::move(cb)),
        expiration_(when),
        interval_(interval),
        repeat_(interval > 0.0),
        sequence_(++s_num_created_) {}
  // 运行timer的callback
  void Run() const { callback_(); }
  // 返回到期的时间戳
  Timestamp Expiration() const { return expiration_; }
  // timer是否是重复触发的
  bool Repeat() const { return repeat_; }
  // timer的创建序号
  int64_t Sequence() const { return sequence_; }
  // 重启timer，如果timer repeat为false，则将时间戳置空
  void Restart(Timestamp now);

  // 当前创建过的timer的数量
  static int64_t NumCreated() { return s_num_created_; }
 private:
  const TimerCallback callback_;
  Timestamp expiration_;
  const double interval_;
  const bool repeat_;
  const int64_t sequence_;

  inline static std::atomic_int64_t s_num_created_ = 0;
};

}  // namespace fio66::net

#endif  // _SRC_NET_TIMER_H_
